﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.CommandUIDefinitionClasses;

namespace RibbonEditor.Controls.CommandUIDefinitions
{
    public partial class InsertTableControl : UserControl, IControlRulable
    {
        private InsertTable innerInsertTable;

        internal InsertTable InnerInsertTable
        {
            get { return innerInsertTable; }
            set { innerInsertTable = value; }
        }

        public InsertTableControl()
        {
            InitializeComponent();
            this.ecCommandType.Values = Enum.GetValues(typeof(Enumerations.CommandType));
            this.innerInsertTable = new InsertTable();
            this.FillControls();
        }

        public InsertTableControl(InsertTable insertTable) : this()
        {
            this.innerInsertTable = insertTable;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.rcAlt.Value = this.innerInsertTable.Alt;
                this.rcCommandPreview.Value = this.innerInsertTable.CommandPreview;
                this.rcCommandRevert.Value = this.innerInsertTable.CommandRevert;
                this.strId.Value = this.innerInsertTable.Id;
                this.rcMenuSectionInitialTitle.Value = this.innerInsertTable.MenuSectionInitialTitle;
                this.rcMenuSectionTitle.Value = this.innerInsertTable.MenuSectionTitle;
                this.icSequence.Value = this.innerInsertTable.Sequence;

                if (this.innerInsertTable.CommandType != null)
                {
                    this.ecCommandType.SelectedValue = this.innerInsertTable.CommandType.Value.ToString();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.innerInsertTable.Alt = this.rcAlt.Value;
                    this.innerInsertTable.CommandPreview = this.rcCommandPreview.Value;
                    this.innerInsertTable.CommandRevert = this.rcCommandRevert.Value;
                    this.innerInsertTable.CommandType = (Enumerations.CommandType)Enum.Parse(typeof(Enumerations.CommandType), this.ecCommandType.SelectedValue);
                    this.innerInsertTable.Id = this.strId.Value;
                    this.innerInsertTable.MenuSectionInitialTitle = this.rcMenuSectionInitialTitle.Value;
                    this.innerInsertTable.MenuSectionTitle = this.rcMenuSectionTitle.Value;
                    this.innerInsertTable.Sequence = this.icSequence.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void InsertTableControl_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.innerInsertTable.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
